package org.nlp2rdf.navigator.component.learn;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.dllearner.core.KnowledgeSource;
import org.dllearner.core.configurators.ComponentFactory;
import org.dllearner.core.configurators.SparqlKnowledgeSourceConfigurator;
import org.dllearner.kb.sparql.SparqlKnowledgeSource;
import org.dllearner.utilities.experiments.Examples;
import org.nlp2rdf.navigator.component.Component;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;
import org.nlp2rdf.navigator.refactored.ISparqlService;

public class KsSPARQLComponent extends Component implements IKnowledgeSources {
	private static final Logger logger = Logger.getLogger(KsSPARQLComponent.class);

	private ISparqlService sparqlService;
	private String cacheDir = null;
	private boolean useLiterals = true;
	private Set<String> predList = null;
	private Set<String> objList = null;
    

	/* (non-Javadoc)
	 * @see org.nlp2rdf.navigator.component.Component#_init()
	 */
	@Override
	protected void _init() throws ComponentInitException {
		try {
			if (cacheDir == null) {
				String msg = "cache dir not set in KsSPARQLComponent";
				logger.error(msg);
				throw new ComponentInitException(msg);
			} else {
				cacheDir = componentConfig.getPathModifier() + cacheDir;
			}

		} catch (Exception e) {
			logger.error("KsSPARQLComponent failed to initialize ",e);
			throw new ComponentInitException(e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.nlp2rdf.navigator.component.learn.IKnowledgeSources#getKnowledgeSources
	 * (org.dllearner.utilities.experiments.Examples)
	 */
	public Set<KnowledgeSource> getKnowledgeSources(Examples examples) {
		check();
		Set<KnowledgeSource> knowledgeSources = new HashSet<KnowledgeSource>();
		SortedSet<String> allInstanceURIs = new TreeSet<String>();
		allInstanceURIs.addAll(examples.getAllExamples());

		SparqlKnowledgeSource ks = ComponentFactory.getSparqlKnowledgeSource(sparqlService.getSparqlTask()
				.getSparqlEndpoint().getURL(), allInstanceURIs);
		SparqlKnowledgeSourceConfigurator conf = ks.getConfigurator();

		conf.setCacheDir(cacheDir);
		conf.setUseCacheDatabase(true);

		conf.setDefaultGraphURIs(new HashSet<String>(sparqlService.getSparqlTask().getSparqlEndpoint()
				.getDefaultGraphURIs()));
		if (objList != null) {
			conf.setObjList(objList);
		}
		if (predList != null) {
			conf.setPredList(predList);
		}
		conf.setUseLits(useLiterals);
		conf.setRecursionDepth(1);
		conf.setCloseAfterRecursion(true);
		conf.setSaveExtractedFragment(true);
		conf.setGetPropertyInformation(false);
		conf.setDissolveBlankNodes(false);

		// TODO set pathmodifier somewhere

		knowledgeSources.add(ks);
		return knowledgeSources;
	}

	public void setSparqlService(ISparqlService sparqlService) {
		this.sparqlService = sparqlService;
	}

	public void setPredList(Set<String> predList) {
		this.predList = predList;
	}

	public Set<String> getPredList() {
		return this.predList;
	}

	public void setObjList(Set<String> objList) {
		this.objList = objList;
	}

	public void setUseLiterals(boolean useLiterals) {
		this.useLiterals = useLiterals;
	}

	public void setCacheDir(String cacheDir) {
		this.cacheDir = cacheDir;
	}

}
